/**
  ******************************************************************************
  * File Name          : route_gw_tonet.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "tskcfg.h"
#include "network.h"
#include "route.h"
#include "cJSON.h"
#include "ctrl_common.h"
#include "storage.h"
#include "collector.h"
#include "dwlcd.h"

static const Route_PayloadType route_to_net_get_topo = {
  .dev_addr  = 0,
  .dev_type  = RT_DV_GW,
  .trans_dir = ROUTE_TRSDIR_LOCAL_TO_NET,
  .pl_type   = ROUTE_PLTYP_GW_GETTP,
  .pl_len    = 0,
  .payload   = {0},
};

static const Route_PayloadType route_to_net_get_sblx = {
  .dev_addr  = 0,
  .dev_type  = RT_DV_GW,
  .trans_dir = ROUTE_TRSDIR_LOCAL_TO_NET,
  .pl_type   = ROUTE_PLTYP_GW_SBLX,
  .pl_len    = 0,
  .payload   = {0},
};

static const Route_PayloadType route_to_net_get_time = {
  .dev_addr  = 0,
  .dev_type  = RT_DV_GW,
  .trans_dir = ROUTE_TRSDIR_LOCAL_TO_NET,
  .pl_type   = ROUTE_PLTYP_GW_TIME,
  .pl_len    = 0,
  .payload   = {0},
};

static const Route_PayloadType route_to_net_cat1info = {
  .dev_addr  = 0,
  .dev_type  = RT_DV_GW,
  .trans_dir = ROUTE_TRSDIR_LOCAL_TO_NET,
  .pl_type   = ROUTE_PLTYP_GW_CAT1INFO,
  .pl_len    = 0,
  .payload   = {0},
};

//static const Route_PayloadType route_to_net_power_on = {
//  .length = sizeof(Route_general_messageType),
//  .linkaddr = 0,
//  .empty1  = 0,
//  .empty2  = 0,
//  
//  .payload[0] = ROUTE_ROOT_LOCAL_TO_NET,
//  .payload[1] = RT_DV_GW,
//  .payload[2] = ROUTE_SUBTYP_GW_POWER,
//  .payload[3] = 0,
//};

/* 关于上下电报警,
   payload可为普通消息,其值表明上电或者掉电,
   但这版只需要报告掉电,顾无负载即可 */
static const Route_PayloadType route_to_net_power_off = {
  .dev_addr  = 0,
  .dev_type  = RT_DV_GW,
  .trans_dir = ROUTE_TRSDIR_LOCAL_TO_NET,
  .pl_type   = ROUTE_PLTYP_GW_POWER,
  .pl_len    = 0,
  .payload   = {0},
};

static const Route_PayloadType route_local_cfg_get_csq = {
  .dev_addr  = 0,
  .dev_type  = RT_DV_GWCFG,
  .trans_dir = ROUTE_TRSDIR_LOCAL_CONFIG,
  .pl_type   = ROUTE_PLTYP_GWCFG_GETCSQ,
  .pl_len    = 0,
  .payload   = {0},
};

void Route_gw_tonet_gettopo(void)
{
	if(Route_EvewToNet_Qhd != NULL)
	{
		xQueueSend(Route_EvewToNet_Qhd, &route_to_net_get_topo, 0);
	}
}

void Route_gw_tonet_addtop(uint8_t index)
{
  Route_PayloadType *proute = NULL;
	
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute != NULL)
  {
    memset(proute, 0, sizeof(Route_PayloadType));

    proute->dev_type  = RT_DV_GW;
    proute->trans_dir = ROUTE_TRSDIR_LOCAL_TO_NET,
    proute->pl_type   = ROUTE_PLTYP_GW_ADDTP,
    proute->pl_len    = sizeof(Route_pl_un_msg_Type);
    
    ((Route_pl_un_msg_Type *)(proute->payload))->msg = index;
    
    if(Route_EvewToNet_Qhd != NULL)
    {
      xQueueSend(Route_EvewToNet_Qhd, proute, 0);
    }
  }
  
  vPortFree(proute);
}

void Route_gw_tonet_deltopo(uint8_t index)
{
  Route_PayloadType *proute = NULL;

  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute != NULL)
  {
    memset(proute, 0, sizeof(Route_PayloadType));

    proute->dev_type  = RT_DV_GW;
    proute->trans_dir = ROUTE_TRSDIR_LOCAL_TO_NET,
    proute->pl_type   = ROUTE_PLTYP_GW_DELTP,
    proute->pl_len    = sizeof(Route_pl_un_msg_Type);

    ((Route_pl_un_msg_Type *)(proute->payload))->msg = index;

    if(Route_EvewToNet_Qhd != NULL)
    {
      xQueueSend(Route_EvewToNet_Qhd, proute, 0);
    }
  }

  vPortFree(proute);
}

void Route_gw_tonet_time(void)
{
	if(Route_EvewToNet_Qhd != NULL)
	{
		xQueueSend(Route_EvewToNet_Qhd, &route_to_net_get_time, 0);
	}
}

void Route_gw_tonet_cat1info(void)
{
	if(Route_EvewToNet_Qhd != NULL)
	{
		xQueueSend(Route_EvewToNet_Qhd, &route_to_net_cat1info, 0);
	}
}

//void Route_gw_tonet_poweron(void)
//{
//	if(Route_EvewToNet_Qhd != NULL)
//	{
//		xQueueSend(Route_EvewToNet_Qhd, &route_to_net_power_on, 0);
//	}
//}

void Route_gw_tonet_poweroff(void)
{
	if(Route_EvewToNet_Qhd != NULL)
	{
		xQueueSend(Route_EvewToNet_Qhd, &route_to_net_power_off, 0);
	}
}

void Route_gw_tonet_sblx(void)
{
	if(Route_EvewToNet_Qhd != NULL)
	{
		xQueueSend(Route_EvewToNet_Qhd, &route_to_net_get_sblx, 0);
	}
}

void Route_gwcfg_local_config_getcsq(void)
{
	if(Route_EvewToNet_Qhd != NULL)
	{
		xQueueSend(Route_EvewToNet_Qhd, &route_local_cfg_get_csq, 0);
	}
}
